﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BattleshipGameClient
{
    public partial class ChatForm : Form
    {
        private String displayName;
        private Connection conn;
        private Uri uri;
        private ChatHistory history;

        public ChatForm(String displayName, Connection conn, Uri uri)
        {
            this.displayName = displayName;
            this.conn = conn;
            this.uri = uri;
            InitializeComponent();
            history = new ChatHistory();
            this.Closing += new CancelEventHandler(Form_Closing);
        }
        private void Form_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            conn.closeChat(uri);
        }
        public void displayMessage(DateTime timestamp, string name, string msg, bool me = false)
        {
            this.Invoke((MethodInvoker)delegate
            {
                history.Add(timestamp, new ChatHistoryElement(name, msg, me));
                messageHistory.Rtf = history.getRtf();
            });
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            DateTime timestamp = new DateTime();
            timestamp = DateTime.UtcNow;
            displayMessage(timestamp.ToLocalTime(), conn.getName(), inpMessage.Text, true);
            conn.selfMessage(uri, inpMessage.Text, timestamp);
            inpMessage.Clear();
        }
    }
}
